package com.topisv.tms.utils.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AopLogs {

	private static final Logger log = LoggerFactory.getLogger(AopLogs.class);
	
	
	// 使用@Pointcut Annotation 时指定切入点表达式
	@Pointcut("execution(* com.topisv.tms.*.*(..))")
	private void anyOldTransfer() {
		
	}

	@Before(value="anyOldTransfer()")
	public void beforeSayHello(JoinPoint joinPoint) {
		Object[] args = joinPoint.getArgs();
		for (int i = 0; i < args.length; i++) {
			log.info("@Before====="+joinPoint.getTarget()+"\t"+joinPoint.toString()+"\t"+args[i]);
		}

	}

	@Around(value="anyOldTransfer()")
	public Object log(ProceedingJoinPoint call) throws Throwable {
		Object object=call.proceed();
		log.info("@Around==="+object);
		return object;
	}

	@After(value="anyOldTransfer()")
	public void afterSayHello(JoinPoint joinPoint) {

		Object[] args = joinPoint.getArgs();
		for (int i = 0; i < args.length; i++) {
			log.info("@After===="+joinPoint.getTarget()+"\t"+joinPoint.toString()+"\t"+args[i]);
		}

	}

	@AfterThrowing(value="anyOldTransfer()", throwing = "ex")
	public void afterThrowingSayHello(Exception ex) {
		log.error("@AfterThrowing====="+ex.getMessage(), ex.getCause());
	}

	@AfterReturning(value="anyOldTransfer()", returning = "reval")
	public void afterReturningSayHello(JoinPoint reval) {
	}
}
